package kyssion.leetcode.util;

public class TreeNode {
    public int val;
    public TreeNode left, right;

    public TreeNode(int val) {
        this.val = val;
        this.left = this.right = null;
    }

    public static TreeNode create(int[] arr) {
        if (arr.length <= 0) {
            return null;
        }
        TreeNode[] treeNodes = new TreeNode[arr.length];
        treeNodes[0] = new TreeNode(arr[0]);

        for (int a = 0; a < arr.length; a++) {
            if (arr[a] != -1) {
                treeNodes[a] = new TreeNode(arr[a]);
                if (a > 0) {
                    if (a % 2 == 0) {
                        treeNodes[(a - 1) / 2].right = treeNodes[a];
                    } else {
                        treeNodes[(a - 1) / 2].left = treeNodes[a];
                    }
                }
            }
        }
        return treeNodes[0];
    }
}
